package com.dlc.shop.platform.task;


import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.dlc.shop.allinpay.service.AllinpayCompanyService;
import com.dlc.shop.bean.dto.BrandShopDTO;
import com.dlc.shop.bean.dto.ShopAuditingInfoDto;
import com.dlc.shop.bean.dto.ShopCreateInfoDTO;
import com.dlc.shop.bean.dto.ShopUserRegisterDto;
import com.dlc.shop.bean.enums.SigningStatus;
import com.dlc.shop.bean.model.*;
import com.dlc.shop.bean.param.AuditingInfoParam;
import com.dlc.shop.bean.vo.BrandShopVO;
import com.dlc.shop.bean.vo.CategoryShopVO;
import com.dlc.shop.bean.vo.ShopCompanyVO;
import com.dlc.shop.common.util.BeanUtil;
import com.dlc.shop.common.util.DateUtils;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.common.util.RedisUtil;
import com.dlc.shop.service.*;
import com.dlc.shop.sys.common.model.ShopEmployee;
import com.dlc.shop.sys.common.service.ShopEmployeeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
//@Component
@AllArgsConstructor
public class ShopRussiaTask {

    private final ShopDetailService shopDetailService;
    private final AllinpayCompanyService allinpayCompanyService;
    private final ShopAuditingService shopAuditingService;
    private final ShopEmployeeService shopEmployeeService;
    private final ShopBankCardService shopBankCardService;
    private final ShopCompanyService shopCompanyService;
    private final CategoryShopService categoryShopService;
    private final BrandShopService brandShopService;
    // 第一次查询所有数据 进行同步

    /**
     * 根据签约时间改变店铺状态
     */
    //@XxlJob("ShopRussiaTask")
    public void ShopRussiaTask() {
        shopDetaliSend(1, 1000);

    }

    public void shopDetaliSend(int current, int pageSize) {
        String stratTimeShopRussia = RedisUtil.get("stratTimeShopRussia");

        ShopCreateInfoDTO shopCreateInfoDTO = new ShopCreateInfoDTO();
        Page<ShopAuditingInfoDto> page = new Page<>();
        AuditingInfoParam auditingInfoParam = new AuditingInfoParam();
        if (stratTimeShopRussia == null || stratTimeShopRussia.equals("")) {
            page.setSize(pageSize);
            page.setCurrent(current);
            auditingInfoParam.setShopStatusList(Arrays.asList(0, 1, 2, 3));
        } else {
            RedisUtil.set("stratTimeShopRussia", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), 0);
            page.setSize(pageSize);
            page.setCurrent(current);
            auditingInfoParam.setBeginTime(DateUtils.strToDateDay(stratTimeShopRussia));
            auditingInfoParam.setEndTime(new Date());
            auditingInfoParam.setShopStatusList(Arrays.asList(0, 1, 2, 3));
        }

        IPage<ShopAuditingInfoDto> shopAuditingInfoDtoIPage = shopAuditingService.auditingInfoList(page, auditingInfoParam);
        for (ShopAuditingInfoDto shopAuditingInfoDto : shopAuditingInfoDtoIPage.getRecords()) {
            Long shopId = shopAuditingInfoDto.getShopId();
            //商家详细信息
            ShopDetail shopDetailByShopId = shopDetailService.getShopDetailByShopId(shopId);
            shopCreateInfoDTO.setShopDetail(BeanUtil.map(shopDetailByShopId, ShopDetail.class));

            //商家注册
            List<ShopEmployee> shopEmployeeList = shopEmployeeService.listEnableEmployee(shopId);
            ShopUserRegisterDto shopUserRegisterInfo = shopCreateInfoDTO.getShopUserRegisterInfo();
            for (ShopEmployee shopEmployee : shopEmployeeList) {
                if (shopEmployee.getType() == 0) {
                    shopUserRegisterInfo.setShopId(shopId);
                    shopUserRegisterInfo.setUsername(shopEmployee.getUsername());
                    shopUserRegisterInfo.setMobile(shopEmployee.getMobile());
                    shopUserRegisterInfo.setPassword("123456");
                }
            }

            //银行卡信息
            List<ShopBankCard> shopBankCards = shopBankCardService.list(new LambdaQueryWrapper<ShopBankCard>().eq(ShopBankCard::getShopId, shopId).eq(ShopBankCard::getStatus, 1));
            shopCreateInfoDTO.setShopBankCardList(shopBankCards);
            //公司信息
            ShopCompanyVO shopCompanyVo = shopCompanyService.getInfo(shopId, 1);
            shopCreateInfoDTO.setShopCompany(BeanUtil.map(shopCompanyVo, ShopCompany.class));

            //签约分类
            List<CategoryShopVO> list = categoryShopService.listSigningCategoryByShopId(shopId);
            List<CategoryShop> categoryShops = new ArrayList<>();
            for (CategoryShopVO categoryShopVO : list) {
                categoryShops.add(BeanUtil.map(categoryShopVO, CategoryShop.class));
            }
            shopCreateInfoDTO.setCategorySigningList(categoryShops);
            //签约品牌列表
            PageParam<BrandShopVO> pageBrand = new PageParam<>();
            pageBrand.setSize(1);
            pageBrand.setCurrent(1000);
            BrandShopDTO brandShop = new BrandShopDTO();
            brandShop.setShopId(shopId);
            IPage<BrandShopVO> brandShopVOIPage = brandShopService.pageSigningByShopId(pageBrand, brandShop);
            List<BrandShopDTO> brandShopDTOS = new ArrayList<>();
            for (BrandShopVO record : brandShopVOIPage.getRecords()) {
                brandShopDTOS.add(BeanUtil.map(record, BrandShopDTO.class));
            }
            shopCreateInfoDTO.setBrandSigningList(brandShopDTOS);


            //调用 https请求 链接进行同步数据
            // 构建body POST请求
            // 请求的URL
            String url = "http://example.com/api/post";

            // 构建POST请求
            HttpRequest request = HttpRequest.post(url)
                    .header("Content-Type", "application/json") // 设置请求头
                    .body(JSON.toJSONString(shopCreateInfoDTO)); // 设置请求体，这里是一个JSON字符串

            // 发送请求，并接收响应
            HttpResponse response = request.execute();

            // 打印响应的内容
            System.out.println(response.body());
        }

        if (shopAuditingInfoDtoIPage.getPages() > current) {
            shopDetaliSend (current + 1, pageSize);
        }
    }

}
